{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Smith Sphere "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The [smith chart](http://en.wikipedia.org/wiki/Smith_chart) is a nomogram used frequently in RF/Microwave Engineering. Since its inception it has been recognised that projecting the chart onto the reimen sphere [1].\n",
    "\n",
    "[1]H. . Wheeler, “Reflection Charts Relating to Impedance Matching,” IEEE Transactions on Microwave Theory and Techniques, vol. 32, no. 9, pp. 1008–1021, Sep. 1984."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#from IPython.display import SVG\n",
    "#SVG('pics/smith_sphere.svg')\n",
    "from galgebra.printer import Format, Fmt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from galgebra import ga\n",
    "from galgebra.ga import Ga\n",
    "from sympy import *\n",
    "Format()\n",
    "\n",
    "(o3d,er,ex,es) = Ga.build('e_r e_x e_s',g=[1,1,1])\n",
    "(o2d,zr,zx) = Ga.build('z_r z_x',g=[1,1])\n",
    "\n",
    "Bz = er^ex # impedance plance \n",
    "Bs = es^ex # reflection coefficient plane\n",
    "Bx = er^es\n",
    "I = o3d.I()\n",
    "\n",
    "def down(p, N):\n",
    "    '''\n",
    "    stereographically project a vector in G3 downto the bivector N\n",
    "    '''\n",
    "    n= -1*N.dual()\n",
    "    return -(n^p)*(n-n*(n|p)).inv() \n",
    "\n",
    "def up(p):\n",
    "    '''\n",
    "    stereographically project a vector in G2 upto the space G3\n",
    "    '''\n",
    "    if (p^Bz).obj == 0:\n",
    "        N = Bz\n",
    "    elif  (p^Bs).obj == 0:\n",
    "        N = Bs\n",
    "        \n",
    "    n = -N.dual()\n",
    "    \n",
    "    return   n + 2*(p*p + 1).inv()*(p-n)\n",
    "    \n",
    "a,b,c,z,s,n = [o3d.mv(k,'vector') for k in ['a','b','c','z','s' ,'n']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Starting with an impedance vector $z$, defined by a vector in the impedance plane  $B_z$, this vector has two scalar components ( $z^r$, $z^x$) known as resistance and reactance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} - \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "-e_s"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Bz.dual()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Bz.is_zero()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} z^{r} \\boldsymbol{e}_{r} + z^{x} \\boldsymbol{e}_{x} \\end{equation*}"
      ],
      "text/plain": [
       "z__r*e_r + z__x*e_x"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = z.proj([er,ex])\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "stereographically  up-projecting this onto the sphere to point $p$, "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{2 z^{r}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{r} + \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{x} + \\frac{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} - 1}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "2*z__r*e_r/(z__r**2 + z__x**2 + 1) + 2*z__x*e_x/(z__r**2 + z__x**2 + 1) + (z__r**2 + z__x**2 - 1)*e_s/(z__r**2 + z__x**2 + 1)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p = up(z)\n",
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(p.norm2())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we stereo-project this back onto the impedance plane"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} z^{r} \\boldsymbol{e}_{r} + z^{x} \\boldsymbol{e}_{x} \\end{equation*}"
      ],
      "text/plain": [
       "z__r*e_r + z__x*e_x"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "down(p, Bz)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} + 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{x} + \\frac{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} - 1}{{\\left ( z^{r} \\right )}^{2} + 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "2*z__x*e_x/(z__r**2 + 2*z__r + z__x**2 + 1) + (z__r**2 + z__x**2 - 1)*e_s/(z__r**2 + 2*z__r + z__x**2 + 1)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "down(p,Bs).simplify()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} - 1}{{\\left ( z^{r} \\right )}^{2} + 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1}  - \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} + 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{r}\\wedge \\boldsymbol{e}_{x} \\end{equation*}"
      ],
      "text/plain": [
       "(z__r**2 + z__x**2 - 1)/(z__r**2 + 2*z__r + z__x**2 + 1) - 2*z__x*e_r^e_x/(z__r**2 + 2*z__r + z__x**2 + 1)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(z-er)*(z+er).inv()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{2 z^{r}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{r} + \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{x} + \\frac{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} - 1}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "2*z__r*e_r/(z__r**2 + z__x**2 + 1) + 2*z__x*e_x/(z__r**2 + z__x**2 + 1) + (z__r**2 + z__x**2 - 1)*e_s/(z__r**2 + z__x**2 + 1)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{\\sqrt{2}}{2}  - \\frac{\\sqrt{2}}{2} \\boldsymbol{e}_{r}\\wedge \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "sqrt(2)/2 - sqrt(2)*e_r^e_s/2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R=((-pi/4)*Bx).exp()\n",
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{- {\\left ( z^{r} \\right )}^{2} - {\\left ( z^{x} \\right )}^{2} + 1}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{r} + \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{x} + \\frac{2 z^{r}}{{\\left ( z^{r} \\right )}^{2} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{s} \\end{equation*}"
      ],
      "text/plain": [
       "(-z__r**2 - z__x**2 + 1)*e_r/(z__r**2 + z__x**2 + 1) + 2*z__x*e_x/(z__r**2 + z__x**2 + 1) + 2*z__r*e_s/(z__r**2 + z__x**2 + 1)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R*p*R.rev()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "\\begin{equation*} \\frac{- {\\left ( z^{r} \\right )}^{2} - {\\left ( z^{x} \\right )}^{2} + 1}{{\\left ( z^{r} \\right )}^{2} - 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{r} + \\frac{2 z^{x}}{{\\left ( z^{r} \\right )}^{2} - 2 z^{r} + {\\left ( z^{x} \\right )}^{2} + 1} \\boldsymbol{e}_{x} \\end{equation*}"
      ],
      "text/plain": [
       "(-z__r**2 - z__x**2 + 1)*e_r/(z__r**2 - 2*z__r + z__x**2 + 1) + 2*z__x*e_x/(z__r**2 - 2*z__r + z__x**2 + 1)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "down(R*p*R.rev(),Bz)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
